package nl.haemers.collections 
{
	import nl.haemers.collections.interfaces.IList;
	import nl.haemers.collections.interfaces.IListIterator;
	
	import nl.haemers.collections.errors.NoSuchElementException;
	import nl.haemers.collections.errors.ConcurrentModificationException;
	import nl.haemers.utils.Logger;
	/**
	 * @author Jonathan Haemers
	 * ListIterator only used by AbstractList
	 */
	public class ListIterator extends Iterator implements IListIterator
	{
		
		public function ListIterator(expectedModCount:int, list:IList, start:int) 
		{
			super(expectedModCount, list);
			
			if (start > -1 && start <= _list.size) 
			{
                cursor = start;
            }
			else 
			{
                throw new RangeError();
            }
		}
		
		/** Public methods **/
		public function add(object:Object) : void
		{
            /*
			if (expectedModCount == modCount) 
			{
			*/
                try 
				{
                    //AbstractList.this.addAt(cursor + 1, object);
					Logger.w("AbstractList.this.addAt(cursor + 1, object);", this);
                } 
				catch (e:RangeError) 
				{
                    throw new NoSuchElementException();
                }
                cursor++;
                lastRet = -1;
                /*if (modCount != expectedModCount) 
				{
                    expectedModCount++;
                }*/
            /*
			} 
			else 
			{
                throw new ConcurrentModificationException();
            }*/
        }
		
        public function hasPrevious() : Boolean
		{
            return cursor > 0;
        }

        public function nextIndex() : int
		{
            return cursor + 1;
        }
		
		
        public function previous() : Object
		{
            /*if (expectedModCount == modCount) 
			{*/
                try {
					var result:Object = _list.get(cursor--);
					lastRet = cursor;
					return result;
				}
				catch (e:RangeError) 
				{
					throw new NoSuchElementException();
				}
			return null;
            /*}
            throw new ConcurrentModificationException();
			*/
        }
		
        public function previousIndex() : int
		{
            return cursor - 1;
        }

        public function set(object:Object) : void
		{
            /*if (expectedModCount == modCount) 
			{*/
                try 
				{
                    //AbstractList.this.set(lastRet, object);
					Logger.w("AbstractList.this.set(lastRet, object);", this);
                }
				catch (e:RangeError) 
				{
                    //throw new IllegalStateException();
					Logger.w("throw new IllegalStateException()", this);
                }
            /*} 
			else
			{
                throw new ConcurrentModificationException();
            }*/
        }

	}
}